package cn.changeforyou.web.blog.service.impl;

import cn.changeforyou.web.blog.luceneAop.Lucene;
import cn.changeforyou.web.blog.luceneAop.LuceneOperate;
import cn.changeforyou.web.blog.po.Article;
import cn.changeforyou.web.blog.po.User;
import cn.changeforyou.web.blog.service.ArticleService;
import cn.changeforyou.web.nutz.common.exception.Asserts;
import cn.changeforyou.web.nutz.common.security.AuthUtil;
import com.alibaba.druid.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.Sqls;
import org.nutz.dao.pager.Pager;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.Mvcs;

import javax.servlet.http.HttpSession;

/**
 * @author zhyu
 */
@IocBean
public class ArticleServiceImpl implements ArticleService {

	@Inject
	Dao dao;
	@Inject
	AuthUtil authUtil;

	@Override
	@Lucene(value = LuceneOperate.select, argsName = {})
	public QueryResult getArticlePage(Pager pager) {
		List<Article> articles = dao.query(Article.class, Cnd.orderBy().desc("id"), pager);
		pager.setRecordCount(dao.count(Article.class));
		return new QueryResult(articles, pager);
	}

	@Override
	public Article getById(int id) {
		List<Article> articles = dao.query(Article.class, Cnd.where("id", "=", id));
		if(articles.isEmpty()) {
			return null;
		}
		return articles.get(0);
	}

    @Override
    @Lucene(value = LuceneOperate.select, argsName = {"keywords", "pager"})
    public QueryResult search(long t, String keywords, Pager pager) {
		Cnd where = Cnd.where("articleName", "like", "%" + keywords + "%")
			.or("articleTag", "like", "%" + keywords + "%")
			.or("articleRemark", "like", "%" + keywords + "%")
			.or("articleContent", "like", "%" + keywords + "%")
			.or("managerName", "like", "%" + keywords + "%");
		List<Article> articles = dao.query(Article.class, where);
		QueryResult queryResult = new QueryResult();
		queryResult.setList(articles);
		pager.setRecordCount(dao.count(Article.class, where));
		queryResult.setPager(pager);
		return queryResult;
    }

	@Override
	public List<String> searchTag() {
		final Set<String> tags = new HashSet<>();
		Set<String> result = new HashSet<>();
		Sql sql = Sqls.create("select articleTag from blog_article");
		sql.setCallback((conn, rs, sql1) -> {
			List<String> list = new LinkedList<String>();
			while (rs.next()) {
				tags.add(rs.getString("articleTag"));
			}
			return list;
		});
		dao.execute(sql);
		for (String tag : tags) {
			String[] split = tag.split(",");
			for (int i = 0; i < split.length; i++) {
				if(!StringUtils.isEmpty(split[i])) {
					result.add(split[i]);
				}
			}
		}
		return new ArrayList<>(result);
	}

	@Override
	public Article saveOrUpdate(Article article) {
		User user = authUtil.getAndValid();
		if(null == article.getId()) {
			article.setCreateTime(LocalDateTime.now());
			article.setManagerId(user.getId());
			article.setManagerName(user.getName());
			Article insert = dao.insert(article);
			if(null == insert) {
				Asserts.fail();
			}
		}else {
			int i = dao.updateIgnoreNull(article);
			if(i != 1) {
				Asserts.fail();
			}
		}
		return article;
	}

    @Override
    public void delete(Integer id) {
		int delete = dao.delete(Article.class, id);
		if(delete != 1) {
			Asserts.fail();
		}
	}
}
